package com.ming.luckyDemo.controller;

import com.ming.luckyDemo.pojo.DO.BetBillDO;
import com.ming.luckyDemo.pojo.DO.UserInfoDO;
import com.ming.luckyDemo.pojo.entity.ResultBody;
import com.ming.luckyDemo.service.BetBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

@RestController
public class BetBillController {

    @Autowired
    private BetBillService betBillService;

    @PostMapping("/saveBet")
    public ResultBody saveBet(HttpServletRequest request,BetBillDO betBillDO){
        Cookie[] cookies = request.getCookies();
        System.out.println("cookie:"+cookies);
        if (Objects.isNull(cookies)){
            return ResultBody.error().message("未登录");
        }
        for (Cookie cookie : cookies) {
            UserInfoDO cookie1 = betBillService.getCookie(cookie.getName());
            if (cookie.getName().equals(cookie1.getUsername()) && cookie.getValue().equals(cookie1.getSessionKey())) {
                betBillDO.setUsername(cookie.getName());
                boolean b = betBillService.selectBalance(betBillDO);
                if (b){
                    return betBillService.saveBet(betBillDO);
                }
                return ResultBody.error().message("余额不足");
            }
        }
        return ResultBody.error().message("cookie错误");
    }

}
